<?php
 class AccountDao extends DaoD
 {
 	protected $pwd;   //密码
 	protected $newAccount;   //新账号
 	protected $accountType;  //账号类型:0,用户名  1，邮箱 2.手机
 	protected $oldpwd;     
 	protected $usertype;      //1为前台用户，2，前台其他用户，0，后台用户
 	protected $vcode=''; //验证码
 	protected $autoLogin=FALSE;
 	public    $mode; //模式
 	
 	public function __construct(array $requestData = array()){
 		parent::__construct($requestData);
 		if (isset($requestData ['User']) && $this->User instanceof User){
 			$this->usertype = $this->User->group_id;
 			$this->pwd      = $this->User->userpwd;
 		}elseif (isset($requestData ['Admin']) && $this->Admin instanceof Admin){
 			$this->usertype = 0;
 		}else{
 			throw new CHttpException(403,'必须包含模型User或者Admin的实例');
 		}
 	}
 	
 	
 	/*
 	 * 添加用户
 	 */
 	public function register(){
 		$this->mode    = $this->mode ? $this->mode:'reg';
 		if ($this->usertype  == 1 || $this->usertype  == 2){
 			$this->User->userpwd  = $this->pwd;
 		    $returnMsg  = $this->User->userRegister($this->mode);
 		    return $returnMsg;
 		} elseif($this->usertype == 0){
 			$this->Admin->pwd = $this->pwd;
 			return $this->Admin->register();
 		}else{
 			throw new CHttpException(403,'用户类型不合法');
 		}
 		
 		
 	}
 	
 	/*
 	 * 用户登陆
 	 */
 	public function login(){
 		if ($this->usertype == 1 || $this->usertype == 2){
 			//前台用户登陆
 			$this->User->userpwd = $this->pwd;
 			$this->User->group_id = $this->usertype;
 			$this->User->VerifyCode = $this->vcode;
 			$this->Uer->autoLogin   = $this->autoLogin;
 			$response = $this->User->login();
 			$flag = UtilD::parseResult($response);
 			return $flag;
 		} else {
 			throw new CHttpException(403,'用户类型不合法');
 		}
 	}
 	
 	
 	
 	
 	/**
 	 * 用户名是否可用
 	 */
 	public function isUsernameCanUse() {
 		$this->User->setScenario('usernameCheck');
 		if ($this->User->validate(array('username'))) {
 			return UtilD::handleResult(1, '用户名可使用');
 		} else {
 			return UtilD::handleResult(-1, '用户名已被注册！' );
 		}
 	
 	
 		//return  $this->User->findUserByAttr("username",$this->User->username);
 	}
 	
 	/**
 	 *  邮箱是否可用
 	 */
 	public function isEmailCanUse() {
 		$this->User->setScenario('emailCheck');
 		if ($this->User->validate(array('email'))) {
 			return UtilD::handleResult(1, '邮箱可使用');
 		} else {
 			return UtilD::handleResult(-1, '邮箱已被注册！');
 		}
 		//return  $this->User->findUserByAttr("email",$this->User->email);
 	}
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 } 